/*----------------
 通用功能
----------------*/
//import { commonData	} from './common.js'

//var common = require('./common')

//在commandData的DeviceList中查找指定的ID
findIdInDeviceList = function(deviceId,commonData){
	for(let i in commonData.deviceList){
		let device_item = commonData.deviceList[i];
		if(device_item.id == deviceId){
			return i;
		}
	}
	return -1;
}

analyDeviceType = function(type){
	//console.log(type)
	let ret = {
		sw_type:0,
		sw_num:0
	}
	if(type == '未知'){
		return ret;
	}
	let t = /([0-9])\*([0-9])S/g.exec(type)
	if (t) {
		//console.log(t)
		ret.sw_type = t[2];
		ret.sw_num = t[1];
		return ret;
	}else{
		console.log('unsupport type:'+type)
		ret.sw_num = -1;
	}
	return ret;
}

switchTypeStr = function(type){
	let ret = analyDeviceType(type);
	if(ret.sw_num==-1){
		return '不支持';
	}else if(ret.sw_num==0 && ret.sw_type == 0){
		return '未知';
	}else{
		return ret.sw_num+'个'+ret.sw_type+'状态开关';
	}
}

module.exports = {
	findIdInDeviceList:findIdInDeviceList,
	analyDeviceType:analyDeviceType,
	switchTypeStr:switchTypeStr
};